package com.lx.ngsl.exam.quest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lx.ngsl.exam.quest.entity.ChoiceEntity;
import com.lx.ngsl.exam.quest.entity.QuestEntity;
import com.lx.ngsl.exam.quest.entity.TagEntity;
import com.lx.ngsl.exam.quest.repository.ChoiceRepository;
import com.lx.ngsl.exam.quest.repository.QuestRepository;
import com.lx.ngsl.exam.quest.service.QuestService;
import com.lx.ngsl.util.UuidUtil;

@RestController
@RequestMapping("/v0")
public class QuestController {
	
	@Autowired
	private QuestService service;
	@Autowired
	private ChoiceRepository a;
	@Autowired
	private QuestRepository b;
	
	private Map<String,String> m = new HashMap<>();

	
	
	@GetMapping("/testjj")
	public void test() {
		m.put("0", "A");
		m.put("1", "B");
		m.put("2", "C");
		m.put("3", "D");
		
		for(int i = 0; i < 80; i++) {
			QuestEntity q = new QuestEntity();
			q.setId(UuidUtil.id());
			q.setQuest("题目" + (i + 1));
			q.setStatus("1");
			q.setType("单选题");
			b.save(q);
			ChoiceEntity c = null; 
			for(int j = 0; j < 4; j++) {
				c = new ChoiceEntity();
				c.setContent("题目" + (i + 1) + "的第" + j + "个选项");
				c.setId(UuidUtil.id());
				c.setInx(m.get(j + ""));
				if(j == 0) {
					c.setIsTrue("1");
				}else {
					c.setIsTrue("0");
					
				}
				c.setQuestId(q.getId());
				a.save(c);
			}
			
		}
		
		for(int i = 0; i < 40; i++) {
			QuestEntity q = new QuestEntity();
			q.setId(UuidUtil.id());
			q.setQuest("题目" + (i + 1));
			q.setStatus("1");
			q.setType("多选题");
			b.save(q);
			ChoiceEntity c = null; 
			for(int j = 0; j < 4; j++) {
				c = new ChoiceEntity();
				c.setContent("题目" + (i + 1) + "的第" + j + "个选项");
				c.setId(UuidUtil.id());
				c.setInx(m.get(j + ""));
				if(j == 0 || j == 3) {
					c.setIsTrue("1");
				}else {
					c.setIsTrue("0");
					
				}
				c.setQuestId(q.getId());
				a.save(c);
			}
			
		}
		
		for(int i = 0; i < 80; i++) {
			QuestEntity q = new QuestEntity();
			q.setId(UuidUtil.id());
			q.setQuest("题目" + (i + 1));
			q.setStatus("1");
			q.setType("判断题");
			b.save(q);
			ChoiceEntity c = null; 
			c = new ChoiceEntity();
			c.setContent("题目" + (i + 1) + "的答案");
			c.setId(UuidUtil.id());
			c.setIsTrue("1");
			c.setQuestId(q.getId());
			a.save(c);
		}
		
	}
	/*
	 * 试题分页查询
	 */
	@GetMapping("/quests")
	public Page<QuestEntity> queryQuestPage(
			@RequestParam(value = "type", required = false, defaultValue = "") String type, 
			@RequestParam(value = "tag", required = false, defaultValue = "") String tag, 
			@RequestParam(value = "quest", required = false, defaultValue = "") String quest, 
			Pageable pageable
			) {
		
		return service.questListPage(type, tag, quest, pageable);
	}
	/*
	 * 试题新增 
	 */
	@PostMapping("/quest")
	public boolean addQuest(
			@RequestParam(value="tag", required = false, defaultValue = "") String tag,
			@RequestParam(value="quest", required = false, defaultValue = "") String quest,
			@RequestParam(value="type", required = false, defaultValue = "") String type,
			@RequestParam(value="choices", required = false, defaultValue = "") String choices,
			@RequestParam(value="answer", required = false, defaultValue = "") String answer,
			@RequestParam(value="img", required = false) MultipartFile img
			) {
		if("判断题".equals(type)) {
			try {
				service.addQuest(tag,quest,type,answer);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}
		
		try {
			service.addQuest(tag,quest,type,choices, img);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 
	 * <p>Method ：delQuest
	 * <p>Description : 试题删除
	 *</p>
	 */
	@DeleteMapping("/quest/{id}")
	public void delQuest(@PathVariable("id") String id) {
		service.del(id);
	}

	/**
	 * 
	 * <p>Method ：queryTags
	 * <p>Description : 标签查询
	 *
	 *</p>
	 */
	@GetMapping("/tags")
	public List<TagEntity> queryTags(){
		return service.queryTags();
	}
	
}
